/*
* Copyright (C) Hisilicon Technologies Co., Ltd. 2016-2019. All rights reserved.
* Description: sample pqcontrol function
* Author: Hisilicon Device IC Solution Technology Development
* Create: 2016/10/27
*/

#include <unistd.h>
#include <string.h>

#include "mpi_awb.h"

typedef struct hiPQT_AWB_NATURA_S {
    HI_S32(*pfnSetSpecAwbAttr)(VI_PIPE ViPipe, const ISP_SPECAWB_ATTR_S *pstSpecAwbAttr);
    HI_S32(*pfnSetCaaControlAttr)(VI_PIPE ViPipe, const ISP_SPECAWB_CAA_CONTROl_S *pstSpecAwbCaaAttr);
    HI_S32  (*pfnSetSpecAwbControlAttr) (VI_PIPE ViPipe, ISP_SPECAWB_CONTROL_ATTR_S *pstSpecAwbControlAttr);
    HI_S32(*pfnGetCaaControlAttr)(VI_PIPE ViPipe, ISP_SPECAWB_CAA_CONTROl_S *pstSpecAwbCaaAttr);
    HI_S32(*pfnGetSpecAwbAttr)(VI_PIPE ViPipe, ISP_SPECAWB_ATTR_S *pstSpecAwbAttr);
    HI_S32  (*pfnGetSpecAwbControlAttr) (VI_PIPE ViPipe, ISP_SPECAWB_CONTROL_ATTR_S *pstSpecAwbControlAttr);
} PQT_AWB_NATURA_S;

#ifdef __USE_SPECAWB__
PQT_AWB_NATURA_S g_stAwbNatura = {HI_MPI_ISP_SetSpecAwbAttr, HI_MPI_ISP_SetCaaControlAttr,\
    HI_MPI_ISP_SetSpecAwbControlAttr, HI_MPI_ISP_GetCaaControlAttr, HI_MPI_ISP_GetSpecAwbAttr,\
    HI_MPI_ISP_GetSpecAwbControlAttr};
#else
PQT_AWB_NATURA_S g_stAwbNatura = {NULL};
#endif

#define FILE_MAX_LENGTH 128

extern int PQControlMain(int argc, char **argv);
//char g_acUserWorkingPath[FILE_MAX_LENGTH] = "/sharefs";
char g_acUserWorkingPath[FILE_MAX_LENGTH] = "/config";

#define ARGS_SIZE_T 10
#define ARG_BUF_LEN_T 256
static char *g_ptaskArgsControl[ARGS_SIZE_T];
static char g_argsBufControl[ARG_BUF_LEN_T];

static unsigned int g_taskidControl = -1;

void PQControl(unsigned long p0, unsigned long p1, unsigned long p2, unsigned long p3)
{
    int i = 0;
    unsigned int argc = p0;
    char** argv = (char**)p1;

    dprintf("\ninput command:\n");

    for (i = 0; i < argc; i++) {
        dprintf("%s ", argv[i]);
    }

    dprintf("\n");
    PQControlMain(argc, argv);
    dprintf("\nmain out\n");
    dprintf("[END]:app_test finish!\n");
    g_taskidControl = -1;
}

void app_control(int argc, char **argv)
{
    int i = 0;
    char* pch = NULL;
    TSK_INIT_PARAM_S stappTask;
    if (argc < 1) {
        dprintf("illegal parameter!\n");
    }

    if (g_taskidControl != -1) {
        dprintf("There's a app_main task existed.");
    }

    memset(&stappTask, 0, sizeof(TSK_INIT_PARAM_S));
    memset(g_argsBufControl, 0, sizeof(g_argsBufControl));
    pch = g_argsBufControl;

    for (i = 0; i < ARGS_SIZE_T; i++) {
        g_ptaskArgsControl[i] = NULL;
    }

    argc++;
    g_ptaskArgsControl[0] = "PQControl";

    for (i = 1; i < argc; i++) {
        int len = 0;
        len = strlen(argv[i - 1]);
        memcpy(pch, argv[i - 1], len);
        g_ptaskArgsControl[i] = pch;
        // keep a '\0' at the end of a string.
        pch = pch + len + 1;

        if (pch >= g_argsBufControl + ARG_BUF_LEN_T) {
            dprintf("args out of range!\n");
            break;
        }
    }

    if (argv[0] && strcmp(argv[0], "stop")) {
        chdir("/");
        memset(g_acUserWorkingPath, 0, FILE_MAX_LENGTH);
        memcpy(g_acUserWorkingPath, argv[0], strlen(argv[0]));
    }

    memset(&stappTask, 0, sizeof(TSK_INIT_PARAM_S));
    stappTask.pfnTaskEntry = (TSK_ENTRY_FUNC)PQControl;
    stappTask.uwStackSize = 0x10000;
    stappTask.pcName = "PQControl";
    stappTask.usTaskPrio = 10;
    stappTask.uwResved = LOS_TASK_STATUS_DETACHED;
    stappTask.auwArgs[0] = argc;
    stappTask.auwArgs[1] = (unsigned long)g_ptaskArgsControl;
    (void)LOS_TaskCreate(&g_taskidControl, &stappTask);

    dprintf("Control_Task %d\n", g_taskidControl);

    chdir(g_acUserWorkingPath);
}
